﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SDFL.Utilities
{
    /// <summary>
    /// Log to text
    /// </summary>
    /// <history>
    ///     [Dylan]     2009-10-19      Created 
    ///     [Dylan]     2009-12-02      Remove the dependency of app.config
    /// </history>
    internal class TextLog: LogBase
    {
        // remove the dependency of app.config
        //private static string _defaultLogFile = Parameters.LogFileName;

        private static string _logFileName = "Results.log";
        private string _logDir;

        public string LogFileName
        {
            get { return _logFileName; }
            set { _logFileName = value; }
        }


        public TextLog()
            : this(_logFileName)
        {
            
        }

        public TextLog(string fileName)
            : this(fileName, false)
        {
        }

        public TextLog(string fileName, bool verbose)
            : base(false)
        {
            string logFile = string.Empty;
            _logDir = System.Environment.CurrentDirectory; 

            logFile = Path.Combine(_logDir, LogFileName);

            if (File.Exists(logFile))
            {
                try
                {
                    File.Delete(logFile);
                }
                catch (Exception)
                {
                }
            }
        }

        public override void WriteEntry(EntryType entryType, string info)
        {
            StreamWriter sw = new StreamWriter(Path.Combine(_logDir, LogFileName), true);
            sw.WriteLine(info);
            sw.Close();
        }

        public override void LogFailure(Exception ex, string info, string bugNumber)
        {
            if (ex == null)
            {
                WriteEntry(EntryType.Failure, "!!!SCENARIO FAILURE!!!" + "\n" +
                 "  Info:       " + info + "\n" +
                 "  Bug #       " + bugNumber + "\n");
            }
            else
            {
                WriteEntry(EntryType.Failure, "!!!SCENARIO FAILURE!!!" + "\n" +
                 "  Exception:  " + ex.ToString() + "\n" +
                 "  Info:       " + info + "\n" +
                 "  Bug #       " + bugNumber + "\n");
            }            
        }

        public override void LogPass(string info)
        {
            WriteEntry(EntryType.Standard, "!!!SCENARIO SUCCESS!!!" + "\n" +
                "   Info:   " + info + "\n");
        }

        public override void LogPass()
        {
            WriteEntry(EntryType.Standard, "!!!SCENARIO SUCCESS!!!" + "\n");
        }


        public override void InitScenario(string scenarioDescription)
        {
            WriteEntry(EntryType.Scenario,
                "\r\n" +
                "************************************************************************************************" + "\r\n" +
                "   Scenario:   " + scenarioDescription + "\r\n" +
                "   Start time: " + DateTime.Now + "\r\n" +
                "************************************************************************************************");
        }

        internal override void InitTestCase(string testcaseDescription)
        {
            WriteEntry(EntryType.Standard,
                "\r\n" +                
                "################################################################################################" + "\r\n" +
                "   Testcase:   " + testcaseDescription + "\r\n" +
                "   Start time: " + DateTime.Now + "\r\n" +
                "################################################################################################");        
        }
    }
}
